#ifndef MY_PROTOCOL_H
#define MY_PROTOCOL_H

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>

#define     FRAME_HEAD                     0xF5

//主控 <--> 主机
#define     CMD_GET_SYS_CONFIG             0xA0   //获取sys_config的信息
#define     CMD_SET_SYS_CONFIG             0xA1   //设置sys_config的信息
#define     CMD_GET_DEV_INFO               0xA2   //获取从机设备节点

//主机 <--> 从机
#define     CMD_SLAVE_NET_IN               0xB0   //从机入网指令
#define     CMD_SLAVE_NET_OUT              0xB1   //从机退网指令
#define     CMD_MASTER_CTRL_SLAVE          0xB2   //主机控制从机指令



#define RET_SUCCESS           0x00   //成功
#define RET_FAIL              0x01   //失败
#define RET_CRC_ERROR         0x02   //CRC错误
#define RET_PWD_ERROR         0x03   //密码错误
#define RET_ADDR_ERROR        0x04   //地址错误
#define RET_TIMEOUT           0x05   //超时


#define UNVARNISHED_TRANSMISSION      0x10        //透传
#define LOCAL_PROCESSING              0x1F        //本地处理


//通信格式结构体
#define MAX_DATA_LEN        256
#define FRAME_HEAD_LEN      5
typedef union UN_COMMUNICATE_BUFF
{
    unsigned char databuff[MAX_DATA_LEN];
    struct
    {
        unsigned char head;
        unsigned char crc_hi;
        unsigned char crc_lo;
        unsigned char len;
        unsigned char cmd;
        unsigned char data[MAX_DATA_LEN-FRAME_HEAD_LEN];
    }Frame;
}TYP_UN_COMMUNICATE_BUFF;


class My_Protocol
{
public:
    My_Protocol();
    ~My_Protocol();

    static unsigned char protocol_data_package(unsigned char cmd,unsigned char len,unsigned char *p_in,TYP_UN_COMMUNICATE_BUFF *p_data);
    static unsigned char protocol_data_unpackage(TYP_UN_COMMUNICATE_BUFF *p_in,unsigned char *p_out);
    static unsigned short get_crc(unsigned char *ptr,unsigned short len);
    static bool check_crc(TYP_UN_COMMUNICATE_BUFF *p_data);

private:


};

#endif
